<?php
/**
 * Created by PhpStorm.
 * User: Lenovo
 * Date: 2018/11/26
 * Time: 13:10
 */

namespace app\api\model;


use think\Exception;
use think\Model;

class Evalutedesc extends Model
{
    protected $hidden=['userid','replyuserid'];
    protected $autoWriteTimestamp = 'datetime';
    protected $updateTime = false;
    /**
     * @desc  增加回复
     * @param $data 回复参数 array
     * @return $re sql执行结果
     */
    public static function addreply($data){
        if(empty($data['replyuserid'])){
            $data['replyuserid']=0;
        }
        try{
            self::startTrans();
            $re=self::create([
                'replyid'=>$data['replyid'],
                'txt'=>$data['txt'],
                'userid'=>$data['userid'],
                'replyuserid'=>$data['replyuserid']
            ]);
            if($data['replyuserid']==0){
                $txt='评论了你';
                $res=Evalute::getEvalute($data['replyid']);
                $belongid=$res->userid;
            }else{
                $txt='回复了你';
                $belongid=$data['replyuserid'];
            }
            Notice::addevalutenotice($data['replyid'],$txt,$data['userid'],$belongid);
            self::commit();
            return $re;
        }catch (Exception $exception){
            self::rollback();
            return false;
        }
    }

    public function user(){
        return $this->belongsTo('User','userid','id')->field('id,name,photo');
    }

    public function replyuser(){
        return $this->belongsTo('User','replyuserid','id')->field('id,name,photo');
    }
}